package com.anrongtec.zcpt.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.anrongtec.zcpt.ApiConfig;
import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.base.BaseActivity;
import com.anrongtec.zcpt.utils.ToolUtils;

public class SplashActivity extends BaseActivity {

    private static final int TIME = 1000;
    private static final int GO_HOME = 1000;
    private static final int GO_GUIDE = 1001;
    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;


    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case GO_HOME:
                    goHome();
                    break;
                case GO_GUIDE:
                    goGuide();
                    break;
            }

        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleShow(false);
        setContentView(R.layout.activity_welcome);
//        if ()
        requestDevicePermission();
    }

    private void requestDevicePermission() {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    MY_PERMISSIONS_REQUEST_CALL_PHONE);
        } else {
            ApiConfig.BaseInfo.APP_OS = ToolUtils.getAppOs();
            ApiConfig.BaseInfo.APP_VERSION = ToolUtils.getAppVersionCode(getApplicationContext()) + "";
            ApiConfig.BaseInfo.DEVICE_SN = ToolUtils.getDeviceSN(getApplicationContext());
            ApiConfig.BaseInfo.DEVICE_TYPE = ToolUtils.getDeviceType();
            ApiConfig.BaseInfo.IMEI = ToolUtils.getIMEI(getApplicationContext());
            ApiConfig.BaseInfo.IMSI = ToolUtils.getIMSI(getApplicationContext());
            ApiConfig.BaseInfo.MAC = ToolUtils.getMAC(getApplicationContext());
            ApiConfig.BaseInfo.IP = ToolUtils.getIpAddr();
            init();
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        //判断请求码，确定当前申请的权限
        if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
            //判断权限是否申请通过
            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //授权成功
                init();
            } else {
                //授权失败
                showToastCustom("此项为必须权限，拒绝将使应用无法正常使用", ApiConfig.WARNING);
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }


    /**
     * 如果是第一次开启进入引导页，如果不是第一次开启则进入登录页
     */
    private void init() {
        SharedPreferences perPreferences = getSharedPreferences("jike", MODE_PRIVATE);
        boolean isFirstIn = perPreferences.getBoolean(ApiConfig.IS_FIRST_LOGIN, true);
        if (!isFirstIn) {
            mHandler.sendEmptyMessageDelayed(GO_HOME, TIME);
        } else {
            mHandler.sendEmptyMessageDelayed(GO_GUIDE, TIME);
            SharedPreferences.Editor editor = perPreferences.edit();
            editor.putBoolean(ApiConfig.IS_FIRST_LOGIN, false);
            editor.apply();
        }

    }


    private void goHome() {
        Intent i = new Intent(SplashActivity.this, LoginActivity.class);
        startActivity(i);
        finish();
    }

    private void goGuide() {
        Intent i = new Intent(SplashActivity.this, GuideActivity.class);
        startActivity(i);
        finish();
    }

}
